package com.operator.Activitys.fragment.base;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.afollestad.materialdialogs.MaterialDialog;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.operator.Activitys.utils.ACache;
import com.zhy.autolayout.config.AutoLayoutConifg;

import org.simple.eventbus.EventBus;

import butterknife.ButterKnife;

/**
 * 项目名称：
 * 类描述：父类公用Fragment
 * 创建人：张飞祥
 * 创建时间：2017/11/12 下午2:17
 * 修改人：张飞祥
 * 修改时间：2017/11/12 下午2:17
 * 修改备注：
 */
public abstract class BaseFragment extends Fragment {
    protected View rootView;
    protected ACache mACache;

    protected abstract void initViews();

    protected abstract int getContentViewId();

    //数据请求加载
    protected MaterialDialog mFrgTimeDialog;
    //内存中用户实体
//    protected EaseUserBean mACacheUser = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(getContentViewId(), container, false);
        //TODO 初始化配置参数
        //数据保存方法
        mACache = ACache.get(getActivity());
        //事件通知
        EventBus.getDefault().register(this);
        //图片加载
        Fresco.initialize(getActivity());
        //黄油刀注册
        ButterKnife.bind(this, rootView);
        //px屏幕适配
        AutoLayoutConifg.getInstance().useDeviceSize();

        //获取内存用户实体
//        Gson gson = new Gson();
//        mACacheUser = gson.fromJson(mACache.getAsJSONObject(Constant.USER) + "",EaseUserBean.class);

        //外部调用方法
        initViews();
        return rootView;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(getActivity());
        EventBus.getDefault().unregister(this);
    }


    public void addFragment(Fragment fragment, int addLayID) {
        FragmentTransaction ft = getActivity().getSupportFragmentManager()
                .beginTransaction();
        ft.add(addLayID, fragment);
        ft.commitAllowingStateLoss();
    }

    /**
     * 显示加载dialog
     */
    public void initShowTimeDialog(String content, Activity activity) {
        //加载进度
        mFrgTimeDialog = new MaterialDialog.Builder(activity)
                .content(content)
                .progress(true, 0)
                .show();
    }


//
//
//    public void removeFragment(Fragment fragment) {
//        FragmentTransaction ft = getActivity().getSupportFragmentManager()
//                .beginTransaction();
//        ft.remove(fragment);
//        //存储状态后进行commit提交会异常,所以要用commitAllowingStateLoss（）
//        ft.commitAllowingStateLoss();
//    }
//
//
//    public void replaceFragment(Fragment fragment, int replaceLayID) {
//        FragmentTransaction ft = getActivity().getSupportFragmentManager()
//                .beginTransaction();
//        ft.replace(replaceLayID, fragment);
//        ft.commitAllowingStateLoss();
//    }
}
